import 'package:args/command_runner.dart';
import 'package:mason_logger/mason_logger.dart';
import 'package:yoyo_get_fast_mirror/i18n.dart';
import 'package:yoyo_get_fast_mirror/src/commands/modules/getfastmirror/getfastmirror.dart';

class GetfastmirrorCommand extends Command<int> {
  GetfastmirrorCommand({
    required Logger logger,
  }) : _logger = logger {
    argParser.addFlag(
      'only-test',
      abbr: 'o',
      help: GetfastmirroCommand.flagOnlyTestHlep,
      negatable: false,
    );
  }

  @override
  String get description => GetfastmirroCommand.description;

  @override
  String get name => 'getfastmirror';

  final Logger _logger;

  @override
  Future<int> run() async {
    final output = lightBlue.wrap(GetfastmirroCommand.runOutPut);
    _logger.info(output);
    final onlyTest = argResults?['only-test'] == true;
    final commandResult = await getFastMirror(onlyTest: onlyTest);
    if (commandResult['success'] == false) {
      _logger.err(commandResult['msg'] as String);
      return ExitCode.unavailable.code;
    }
    return ExitCode.success.code;
  }
}
